<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Stok_titik_penjualan Extends CI_Controller{
	
	function Stok_titik_penjualan()
	{
		parent::__construct();
		$this->load->model('sapi_model');
		$this->load->model('stok_titik_penjualan_model');
		$this->load->model('stok_model');
		$this->load->model('Home_model');
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE){
			redirect('auth');
		}else{
			$id_role = $this->session->userdata('id_role'); 
			$data = array(
				"main_view" 		=> "Stok_titik_penjualan/Stok_titik_penjualan_view",
				"title"				=> "Manajemen Stok Titik Penjualan",
				"jenis_sapi"		=> $this->sapi_model->get_sapi(),
				"titik_penjualan"	=> $this->stok_titik_penjualan_model->get_titik_penjualan(),
				"alasan"			=> $this->stok_titik_penjualan_model->get_alasan(),
				"hak_akses0"		=> $this->Home_model->get_hak_akses($id_role,0),
				"hak_akses1"		=> $this->Home_model->get_hak_akses($id_role,1),
				"hak_akses2"		=> $this->Home_model->get_hak_akses($id_role,2)
			);
			
			$all_stok_titik_penjualan = $this->stok_titik_penjualan_model->get_all_stok_titik_penjualan();
			$data['all_stok_titik_penjualan'] = $all_stok_titik_penjualan->result_array();
			
			$this->load->view('template', $data);
		}
		
	}
	
	function add_stok_titik_penjualan(){
	
		$tgl_kejadian = $this->input->post("tanggal_kejadian");
		$tgl = explode('/', $tgl_kejadian);
		if(count($tgl) > 1) $tgl_kejadian = $tgl[2]."-".$tgl[0]."-".$tgl[1];
		
		$input_jumlah = $this->input->post("jumlah");
		$id_alasan = $this->input->post("alasan");
		
		$alasan = $this->stok_model->get_alasan_by_id($id_alasan);
		
		if($alasan->JENIS_ALASAN == 0){ //pengurangan
			$jumlah = $input_jumlah * -1;
		}
		
		else if($alasan->JENIS_ALASAN == 1){ //penambahan
			$jumlah = $input_jumlah;
		}
		
		$data_stok = array(
				'ID_USER'			=> $this->session->userdata('id_user'),
				'ID_TITIK_PASOK' 	=> $this->input->post('titik_penjualan'),
				'ID_SAPI' 			=> $this->input->post('sapi'),
				'ALASAN_PERUBAHAN' 	=> $id_alasan,
				'JUMLAH' 			=> $jumlah,
				'TANGGAL_KEJADIAN' 	=> $tgl_kejadian,
				'KOMENTAR' 			=> $this->input->post('catatan')
			);
			
		$this->stok_titik_penjualan_model->add_stok_titik_penjualan($data_stok);
		
		redirect('stok_titik_penjualan');
	
	}
	
	
}